package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.City;
import com.yoyo.itravel.bean.PageBean;
import com.yoyo.itravel.constants.MsgConstant;
import com.yoyo.itravel.dao.ICityDao;
import com.yoyo.itravel.exceptions.CityException;
import com.yoyo.itravel.service.ICityService;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 城市相关操作服务层接口实现类
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/18 15:43
 */
@Service
public class CityServiceImpl implements ICityService {

    @Autowired
    private ICityDao cityDao;

    /**
     * @Description 添加城市
     * @Param city
     * @Return boolean
     */
    @Override
    public boolean addCity(City city) throws CityException {
        // 判断city对象是否为null
        if (city == null) {
            throw new CityException("传入的城市对象为null...");
        }

        try {
            /** 1.判断该城市名称是否已经存在 **/
            if (cityDao.selectByName(city.getCityName()) != null) {
                // 城市已存在，不可新增城市
                return false;
            }

            /** 2.新增城市 **/
            return cityDao.insert(city) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CityException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @Description 查找所有的城市
     *
     * @Param
     * @Return java.util.List<com.yoyo.itravel.bean.City>
     */
    @Override
    public List<City> findAllCity() throws CityException {
        try {
            return cityDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CityException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @Description 分页查询城市
     * @Param
     * @Return java.util.List<com.yoyo.itravel.bean.City>
     */
    @Override
    public List<City> findCityByPage(PageBean page) throws CityException {
        try {
            return cityDao.selectCityByPage(page);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CityException("分页查询城市失败！");
        }
    }

    /**
     * @Description 通过省份id查找该省份下的所有城市
     *
     * @Param provinceId
     * @Return java.util.List<com.yoyo.itravel.bean.City>
     */
    @Override
    public List<City> findCityByProvinceId(String provinceId) throws CityException {
        try {
            return cityDao.selectByProvinceId(provinceId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CityException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @Description 通过城市名称查找城市
     *
     * @Param city
     * @Return com.yoyo.itravel.bean.City
     */
    @Override
    public City findByName(String city) throws CityException {
        if (StringUtils.isEmpty(city)) {
            throw new CityException("请选择城市！");
        }
        try {
            return cityDao.selectByName(city);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CityException(MsgConstant.INTERNAL_ERR);
        }
    }

    /**
     * @Description 查询城市总数量
     *
     * @Param
     * @Return java.lang.Integer
     */
    @Override
    public Integer totalCount() throws CityException{
        try {
            return cityDao.totalCount();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CityException(MsgConstant.INTERNAL_ERR);
        }
    }
}
